#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <stack>
#include <cstring>
#include <queue>
#include <cctype>
#include <iomanip>
#include <climits>
#include <array>

using i64 = signed long long;

constexpr int N = 5E3 + 5;
constexpr int INF = 0x3f3f3f3f;

std::vector<std::array<int, 2>> adj[N];

i64 prim(int n) {
    std::vector<int> st(n + 1, false);
    std::vector<int> cost(n + 1, INF);
    std::priority_queue<std::array<int, 2>, std::vector<std::array<int, 2>>, std::greater<>> heap;

    heap.push({0, 1});
    cost[1] = 0;
    while (heap.size()) {
        auto [d, u] = heap.top();
        heap.pop();

        if (st[u]) continue;
        st[u] = true;

        for (auto [v, w] : adj[u]) {
            if (!st[v] && cost[v] > w) {
                cost[v] = w;
                heap.push({w, v});
            }
        }
    }

    i64 re = 0;
    for (int i = 1; i <= n; ++i) {
        if (cost[i] == INF) {
            return -1;
        }
        re += cost[i];
        // std::cout << cost[i] << " \n"[i == n];
    }
    return re;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, m;
    std::cin >> n >> m;

    for (int i = 0; i < m; ++i) {
        int u, v, w;
        std::cin >> u >> v >> w;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }

    auto re = prim(n);
    if (re == -1) {
        std::cout << "orz\n";
    } else {
        std::cout << re << "\n";
    }

    return 0;
}
